import os
import sys
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg19 import preprocess_input
from keras.applications.vgg19 import decode_predictions
from keras.applications.vgg19 import VGG19
from keras.models import Model
import numpy as np
import json
import cv2
import time
import csv
from pymongo import MongoClient
import pymongo
img_path = 'E:\\image_pi\\'
videolist={}
client = MongoClient(port=27017)
db=client['tiktok']
i=0
namepi='toplist0'
tmax=1500
t=0
with open('E:\\data_pi\\top100list.tsv','r', encoding='utf-8',newline='\n') as fin:
    reader = csv.reader(fin, delimiter='\t')
    for line in reader:
        col = line[0]
        k = line[1]
        item = db[col].find_one({'_id': k})
        if len(item['video_feature']['img_embed']) == 0:
            if col not in videolist.keys():
                videolist[col]=[]
            videolist[col].append(item['_id'])
            t+=1
            if t>tmax:
                break
c=0
for hashtag in videolist.keys():
    for vid in videolist[hashtag]:
        c+=1
        if not os.path.exists('E:\\Tiktok\\Hashtag\\' + hashtag + '\\' + vid + '.mp4'):
            continue
        vname=vid

        vidcap = cv2.VideoCapture('E:\\Tiktok\\Hashtag\\' + hashtag + '\\' + vid + '.mp4')
        count = 0
        vidcap.set(cv2.CAP_PROP_POS_MSEC, (count * 5000))
        success, image = vidcap.read()

        while success:
            vidcap.set(cv2.CAP_PROP_POS_MSEC, (count * 5000))
            if os.path.exists(img_path + vname + "_%d.jpg" % count+'.jpg'):
                count+=1
                continue
            cv2.imwrite(img_path + vname + "_%d.jpg" % count, image[round(len(image)/10):round(len(image)-len(image)/10)])
            success, image = vidcap.read()
            count = count + 1
            if count>12:
                break
idlist=[]
for ht in videolist.keys():
    idlist.extend(videolist[ht])
with open('E:\\data_pi\\imglist_temp'+namepi+'.txt','w') as file_input:
    c=0
    for vname in os.listdir(img_path):
        v=vname.split('_')[0]
        if vname.endswith('.jpg') and v in idlist:
            c+=1
            file_input.write(img_path+vname+'\n')
    print(c)
os.system('"'+'py -2 D:\\Work\\kusuri\\DeepSentiBank\\sentiBank.py '+'E:\\data_pi\\imglist_temp'+namepi+'.txt'+'"')


base_model = VGG19(weights='imagenet')
model = Model(base_model.input, base_model.get_layer('fc2').output)
col=''
with open ('E:\\data_pi\\imglist_temp'+namepi+'.txt','r',) as txt_input:
    with open ('E:\\data_pi\\imglist_temp'+namepi+'.json','r',) as json_input:
        senti=json.load(json_input)
        c=0
        prev=''
        l=0
        avg=[]
        for line in txt_input:
            line=line.replace(img_path,'')
            print(line,c)
            iid=line.split('_')[0]
            imgnum=line.split('_')[1].replace('.jpg','').strip()
            for ht in videolist.keys():
                if iid in videolist[ht]:
                    col=ht
                    break
            ori = db[col].find_one({'_id': iid})
            if imgnum in ori['video_feature']['img_embed'].keys():
                c+=1
                continue
            print(iid)
            tempresult=[]
            line=line.strip()
            img = load_img(img_path+line,target_size=(224, 224))
            img = img_to_array(img)
            img = np.expand_dims(img, axis=0)
            img = preprocess_input(img)
            fc2_features = model.predict(img)
            prob_vgg = decode_predictions(base_model.predict(img), top=1000)
            for item in sorted(prob_vgg[0]):
                tempresult.append(float(item[2]))
            for item in sorted(senti['images'][c]['bi-concepts']):
                tempresult.append(float(senti['images'][c]['bi-concepts'][item]))
            for item in senti['images'][c]['features']:
                tempresult.append(float(item))
            for ht in videolist.keys():
                if iid in videolist[ht]:
                    col=ht
                    break
            db[col].update_one({'_id': iid}, {'$set':{'video_feature.img_embed.'+imgnum:tempresult}})
            print(db[col].find_one({'_id': iid}))
            c+=1
